package com.ruoyi.web.sso.api;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.sso.domain.GroupInfo;
import com.ruoyi.web.sso.service.IGroupInfoService;
import com.ruoyi.web.sso.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * 集团详情信息Controller
 * 
 * @author qiufeng
 * @date 2022-06-23
 */
@Controller
@RequestMapping("/api/sso/groupInfo")
public class ApiGroupInfoController extends BaseController
{

    @Autowired
    private IGroupInfoService groupInfoService;

    @Autowired
    private OssService ossService;

    @Value("${oss.path}")
    public  String path;
    /**
     * 查询集团详情信息列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GroupInfo groupInfo)
    {
        startPage();
        List<GroupInfo> list = groupInfoService.selectGroupInfoList(groupInfo);
        return getDataTable(list);
    }

    /**
     * 新增保存集团详情信息
     */
    @Log(title = "集团详情信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GroupInfo groupInfo, MultipartFile file)
    {
        if (Objects.nonNull(file)){
            String originalName = file.getOriginalFilename();
            String extension = originalName.substring(originalName.lastIndexOf("."));
            String fileName = "admin" + "_"+ System.currentTimeMillis() + extension;
            String url = ossService.aliyunOssUpload(path+ "common/"+ fileName,file);
            groupInfo.setUrl(url);
        }
        return toAjax(groupInfoService.insertGroupInfo(groupInfo));
    }



    /**
     * 修改保存集团详情信息
     */
    @Log(title = "集团详情信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GroupInfo groupInfo, MultipartFile file)
    {
        if (Objects.nonNull(file)){
            String originalName = file.getOriginalFilename();
            String extension = originalName.substring(originalName.lastIndexOf("."));
            String fileName = "admin" + "_"+ System.currentTimeMillis() + extension;
            String url = ossService.aliyunOssUpload(path+ "common/"+ fileName,file);
            groupInfo.setUrl(url);
        }
        return toAjax(groupInfoService.updateGroupInfo(groupInfo));
    }

}
